package com.wyq.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wyq.bean.Audience_Qtitle;
import com.wyq.service.AudienceQService;

/**
 * 问卷调查标题
 * @author Fyj
 */
public class QTitleManagerAction extends ActionSupport {
	private AudienceQService audienceQService;
	private String showId;
	
	private String formStr;
	private String id;//选择删除的行的IDs
	
	//获取所有的调查部卷标题
	public String getAllQTitle() throws Exception {
		ActionContext context = ActionContext.getContext();   
		HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out=response.getWriter();
		
		List list = audienceQService.findAllQTitle(showId);
		String jsonStr = "{qTitleList:" + JSONArray.fromObject(list).toString() + "}"; 
		
		out.print(jsonStr);
		
		return null;
		
	}
	
	//保存
	public String saveQTitle(){
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out=response.getWriter();
			
			formStr = java.net.URLDecoder.decode(formStr,"UTF-8");
			System.out.println("++++++++++++++++" + formStr);
			Map qTitlemap = (Map)JSONUtil.deserialize(formStr);
			qTitlemap.put("showId", showId);
			int flag = -1;
			int saveFlag = -1;
			if(qTitlemap.get("id").equals("")){//新增
				saveFlag = 0;
			}else if(!qTitlemap.get("id").equals("")){
				saveFlag = 1;
			}
			
			try {
				flag = audienceQService.saveAudienceQtitle(qTitlemap, saveFlag);
				out.print("{success:true,data:'" + flag + "'}");
				
			} catch (Exception e) {
				e.printStackTrace(); 
				out.print("{success:false,reason:'保存失败'}");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	//删除
	public String deleteQTitle(){
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out=response.getWriter();
			
			try{
				String[] idArr = id.split(",");
				for(int i=0;i<idArr.length;i++){
					if(null != idArr[i] && !"".equals(idArr[i])){
						Audience_Qtitle audienceQtitle = new Audience_Qtitle();
						audienceQtitle.setId(Integer.parseInt(idArr[i]));
						audienceQService.removeAudienceQtitle(audienceQtitle);
					}
				}
				out.print("{success:true,data:'操作成功'}");
			}catch(Exception e){
				e.printStackTrace();
				out.print("{success:false,reason:'删除失败'}");
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	

	public void setAudienceQService(AudienceQService audienceQService) {
		this.audienceQService = audienceQService;
	}

	public void setFormStr(String formStr) {
		this.formStr = formStr;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public void setShowId(String showId) {
		this.showId = showId;
	}



}
